<?php

class KlachtController extends Zend_Controller_Action
{
        
        protected $_authSession;
        protected $_user;
        
    public function init()
        {
                if (Zend_Session::namespaceIsset('Zend_Auth') ){
                        $this->_authSession = new Zend_Session_Namespace('Zend_Auth');
                }
                
                $userModel = new Model_DbTable_User();
                $this->_user = $userModel->getUserInfo($this->_authSession->userId);
                                        
                if($this->_user['userGroup'] === "Administrator" || $this->_user['userGroup'] === "Medewerker"){

            $this->view->editableUser = true;
                        
                }
        }
                        
    public function indexAction()
        {
                $this->_forward('list');
        }

        public function addAction()
        {
                $form = new Form_Klacht();
                $form->submit->setLabel('Add');
                $form->klachtEigenaar->setValue($this->_authSession->userId);           
                $this->view->form = $form;
                                        
                if ( $this->getRequest()->isPost() ) {
                                                
                        $formData = $this->getRequest()->getPost();
                        
                        if ($form->isValid($formData)) {
                                
                                $data = array (
                                        'klachtTitel'       => $form->getValue('klachtTitel'),
                                        'klachtBody'        => $form->getValue('klachtBody'),
                                        'klachtEigenaar'    => $form->getValue('klachtEigenaar'),
                                		'klachtKort'		=> substr($form->getValue('klachtBody'),0 ,100)
                                );
                
                                $klacht = new Model_DbTable_Klacht();
                                $klacht->insert($data);
                                
                                $this->_helper->redirector( 'list' );

                        } else {
                                $form->populate( $formData );
                        }

                } 
                
        }

        public function listAction()
        {
                $this->view->title = "Klachten";
                $this->view->headTitle($this->view->title);
                
                $klachtModel = new Model_DbTable_Klacht();
                
                if($this->_user['userGroup'] === "Gebruiker"){
                	//dit is voor de user
                    $klacht = $klachtModel->fetchUserKlachten($this->_authSession->userId);
                } elseif($this->_user['userGroup'] === "Manager"){
                	//dit is voor de Manager
                } elseif ($this->_user['userGroup'] === "Medewerker") {
                	//dit is voor de medewerker
                	
                	//alle klachten per status ophalen (uiteindelijk wil je dit in een loopje halen)
                	$klachtStatusOpen 		= $klachtModel->fetchMedewerkerKlachten(1);
                	$klachtStatusProcess 	= $klachtModel->fetchMedewerkerKlachten(2);
                	$klachtStatusClosed 	= $klachtModel->fetchMedewerkerKlachten(3);
                	
                	$klacht = array(
                					array("open", $klachtStatusOpen), 
                					array("process", $klachtStatusProcess), 
                					array("closed", $klachtStatusClosed)
                			  );
                	//juiste viewscript laden
                	$this->_helper->viewRenderer('listmedewerker');
                	
                } elseif ($this->_user['userGroup'] === "Administrator") {
                	//dit is voor de administrator
                    $klacht = $klachtModel->fetchAllKlachten();             
                } else {
                	throw new exception('Failed to find tourguide');
                }

                $this->view->klacht = $klacht;
        }
        
        public function editAction()
        {
                $request = $this->getRequest();
                $callid = $request->getParam('callid', 0);

                if ($callid === 0) {
                        throw new exception('failed to find call ID number');
                }
                
                $this->view->title = "Edit klacht";
                $this->view->headTitle($this->view->title);
                
                //juiste form laten zien
                if($this->_user['userGroup'] === "Administrator" || $this->_user['userGroup'] === "Medewerker"){
                        $form = new Form_AdminKlacht($this->_authSession->userGroup);
                        $form->setAction("/klacht/edit/callid/" . $callid);
                        $form->submit->setLabel('Save');        
                } else {
                        $form = new Form_Klacht($this->_authSession->userGroup);
                        $form->setAction("/klacht/edit/callid/" . $callid);
                        $form->submit->setLabel('Save');
                }
                
                if ($this->getRequest()->isPost()) {
                        
                        $formData = $this->getRequest()->getPost();
                        
                        if ($form->isValid($formData)) {

                                $data = array (
                                        'klachtTitel'      		=> $form->getValue('klachtTitel'),
                                        'klachtBody'     		=> $form->getValue('klachtBody'),
                                        'klachtStatus'          => $form->getValue('klachtStatus'),
										'klachtPrio'          	=> $form->getValue('klachtPrio'),
										'klachtConfig'          => $form->getValue('klachtConfig'),
										'klachtType'          	=> $form->getValue('klachtType'),
										'klachtTijdsduur'      	=> $form->getValue('klachtTijdsduur'),
										'klachtOplosDatum'      => $form->getValue('klachtOplosDatum'),
										'klachtOplossing'      	=> $form->getValue('klachtOplossing'),
										'klachtBehandelaar'     => $form->getValue('klachtBehandelaar'),
										'klachtKort'      		=> $form->getValue('klachtKort'),
                                );

                                $klacht = new Model_DbTable_Klacht();
                                $klacht->updateKlacht($callid, $data);

                                $this->_helper->redirector( 'index' );

                        } else {
                                throw new exception("You suck at filling forms");
                        }
                        
                } else {

                                $klacht = new Model_DbTable_Klacht();

                                $row = $klacht->find( intval( $callid ) );
                                $data = $row->toArray();
                                
                                $form->populate($data[0]);

                }
                $this->view->form = $form;
        }
        
        
        
        public function deleteAction()
                        {
                                $this->view->title = "Delete klacht";
                                $this->view->headTitle($this->view->title);
                                
                                
                                if ($this->getRequest()->isPost()) {
                                        
                                        $del = $this->getRequest()->getPost('del');
                                        if ($del == 'Yes') {
                                                $callid = $this->getRequest()->getPost('callid');
                                                $klacht = new Model_DbTable_Klacht();
                                                $klacht->deleteKlacht( $callid );
                                                
                                        }

                                        $this->_helper->redirector( 'index' );

                                } else {
                                        $callid = $this->_getParam( 'callid', NULL );
                                        
                                        if( !is_null( $callid ) ){
                                                $klacht = new Model_DbTable_Klacht();
                                                $rowset = $klacht->find( intval( $callid ) );
                                                $this->view->klacht = $rowset->current();
                                        }
                                }
                        }
}
